package backend_package;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import database_package.SqliteDatabase;

/**
 * The Analyst class that deals with the Database and AnalystPanel class.
 * It gets the data from database and provides the AnalystPanel class with 
 * that data. Uses Observer design pattern.
 * @author Eva.
 * @author Syrym edited.
 * @version 12/12/2014
 */
public class Analyst extends Observable implements Observer {
	/**The list of notes for the user.
	 * @author Eva
	 * */
	List<Notes> myNotes;
	/**The user name.
	 * @author Eva
	 * */
	String myUserName;
	
	/**The list of assigned RFPs for the user.
	 * @author Eva
	 * */
	public ArrayList<String> myAssignedRFPs;
	/**The map of RFPs and Drafts for the user.
	 * @author Eva
	 * */
	public Hashtable<String, String> myWorkingRFPs;
	/**The Analyst constructor.
	 * @author Eva
	 * */
	public Analyst() {
		
		myAssignedRFPs = new ArrayList<String>();
		myWorkingRFPs = new Hashtable<String, String>();
		
		myNotes = new ArrayList<Notes>();
		
	}
	

	/**
	 * 
	 * @author Eva Revear
	 * 
	 * @date 12-1-14
	 * 
	 * gets a list locations where rfps are stored from the sqlite database and populate the panel with options
		of files to open; called in login; passes in analyst name and makes a list of the locations of his rfp files
	 * 
	 * @param theRFPs a list of rfps from the database
	 * @return a two dimensional array of the rfps that are passed to the GUI
	*/
	String[][] getRFPs(ArrayList<String> theRFPs) {
		myAssignedRFPs = theRFPs;
		String[][] assignedRFPs = new String[theRFPs.size()][1];
		for( int i = 0; i < myAssignedRFPs.size(); i++) {
			assignedRFPs[i][0] = myAssignedRFPs.get(i);
		}
		return assignedRFPs;
	}
	
	/**
	 * 
	 *  @author Eva Revear
	 *  @date 12-10-14
	 *  
	 *  @param analyst sets the name of the currently logged in user
	 *  
	 *  */
	void setUserName(String analyst) {
		myUserName = analyst;
	}
	
	/**
	 * 
	 *  @author Eva Revear
	 *  
	 *  @date 12-1-14
	 *  
	 *  @return a list of RFPs as strings
	 *  */
	public ArrayList<String> returnRFPs() {
		return myAssignedRFPs;
	}
	
	/**
	 * 
	 *  @author Eva Revear
	 *  
	 *  @date 12-1-14
	 *  
	 *  @return a logged in user as a string
	 *  */
	public String getUserName() {
		return myUserName;
	}
	
	/**
	 * 
	 * @author Eva Revear
	 * @author Sryrym Satanov
	 *  
	 *  
	 *  @date 12-1-14
	 *  
	 *  @param theDrafts a hashtable of draft file locations passed from the database
	 *  @return a 2D array of Strings representing draft file locations
	 *  */
	String[][] getDraft(Hashtable<String, String> theDrafts) {
		myWorkingRFPs = theDrafts;
		String[][] drafts = new String[theDrafts.size()][2];
		Set<String> rfps =  myWorkingRFPs.keySet();
		int i = 0;
		for(String rfp: rfps) {
			drafts[i][0] = rfp;
			drafts[i][1] = myWorkingRFPs.get(rfp);
			i++;
		}
		return drafts;
	}
	@Override
	
	/**
	 * 
	 * @author Eva Revear
	 * @author Sryrym Satanov
	 *  
	 *  
	 *  @date 12-1-14
	 *  
	 *  @param o an observable object that passes in some information through a packet
	 *  @param arg the arguments passed in by the object being observed by this class
	 *  
	 *  This method takes in information about  the analyst from the database and login panel
	 *  it also notifies its observers of any changes in the analyst
	 *  */
	public void update(Observable o, Object arg) {
		Packet packet = (Packet)arg;

		if( o instanceof Login &&  packet.getTag().equals("l_analystchosen_a")) {

			setChanged();
			notifyObservers(new Packet("a_getuserdata")); //notifies DB
		}
		if(o instanceof SqliteDatabase && packet.getTag().equals("db_userdata_a")) {

			@SuppressWarnings("unchecked")
			String[][] assignedRFPs = getRFPs((ArrayList<String>)packet.get(0));
			@SuppressWarnings("unchecked")
			String[][] drafts = getDraft((Hashtable<String, String>)packet.get(1));
			myUserName = packet.getUsername();
			setUserName(myUserName);

			Object [] oarr = {assignedRFPs, drafts};
			
			setChanged();
			notifyObservers(new Packet("a_userdata_ap", oarr, packet.getUsername())); //notifies analystpanel
			
			setChanged();
			notifyObservers(new Packet("a_username_qp", packet.getUsername()));//notifies the Questions Panel
		}

		
	}
	
	
	
	
}
